/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.bc;

import edu.upn.sigemfi.vfi.be.Transferencia;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class TransferenciaDelegate {

        private TransferenciaFacadeLocal local;

    public TransferenciaDelegate() {
        local = lookupTransferenciaFacade();
    }

    public void crear(Transferencia transferencia) throws Exception {
        try {
            local.create(transferencia);
        } catch (Exception e) {
            local = lookupTransferenciaFacade();
            throw new Exception("Crear Transferencia. " + e.getMessage(), e);
        }
    }

    public void editar(Transferencia transferencia) throws Exception {
        try {
            local.edit(transferencia);
        } catch (Exception e) {
            local = lookupTransferenciaFacade();
            throw new Exception("Editar Transferencia. " + e.getMessage(), e);
        }
    }

    public void eliminar(Transferencia transferencia) throws Exception {
        try {
            local.remove(transferencia);
        } catch (Exception e) {
            local = lookupTransferenciaFacade();
            throw new Exception("Eliminar Transferencia. " + e.getMessage(), e);
        }
    }

    public Transferencia buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupTransferenciaFacade();
            throw new Exception("Buscar Transferencia. " + e.getMessage(), e);
        }
    }

    public List<Transferencia> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupTransferenciaFacade();
            throw new Exception("Listar Transferencia. " + e.getMessage(), e);
        }
    }


    private TransferenciaFacadeLocal lookupTransferenciaFacade() {
        try {
            Context c = new InitialContext();
            return (TransferenciaFacadeLocal) c.lookup("java:comp/env/TransferenciaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
